package com.sky.controller.admin;

import com.sky.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

@RestController("adminShopController")
@RequestMapping("/admin/shop")
@Slf4j
@Api(tags = "店铺相关接口")
public class ShopController {

    public static final String KEY = "SHOP_STATUS";

    @Autowired
    private RedisTemplate redisTemplate;


    /**
     * 设置营业状态
     * @param status
     * @return
     */
    @PutMapping("/{status}")
    @ApiOperation("设置营业状态")
    public Result setStatus(@PathVariable Integer  status){
        log.info("设置营业状态：{}", status==1?"营业中":"打烊中");
        redisTemplate.opsForValue().set(KEY, status.toString());
        return Result.success();
    }

    /**
     * 查询营业状态
     * @return
     */
    @GetMapping("/status" )
    @ApiOperation("查询营业状态")
    public Result<Integer> getStatus(){
        Object statusObj = redisTemplate.opsForValue().get(KEY);
        Integer status = null;

        if (statusObj != null) {
            try {
                status = Integer.valueOf(statusObj.toString());
            } catch (NumberFormatException e) {
                log.error("转换营业状态失败: {}", statusObj);
            }
        }

        log.info("查询营业状态:{}", status == null ? "未设置" : (status == 1 ? "营业中" : "打烊中"));
        return Result.success(status);
    }
}
